﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace GraphTest.Geometry2D
{
    public class Geometry2DHelper
    {
        public static Vector2D FindMaxUncollisionShift(Rectangle2D r, Rectangle2D r0, Vector2D shift)
        {
            Vector2D result = shift;

            Segment2D[] rSegments = r.GetSegments();
            Segment2D[] shiftSegments = r0.GetShiftSegments(shift);

            Vector2D minShift = shift;

            foreach (var rSegment in rSegments)
            {
                foreach (var shiftSegment in shiftSegments)
                {
                    if (Segment2D.IsSegmentsIntersected(rSegment, shiftSegment))
                    {
                        Point2D intersection = Segment2D.GetIntersectionPoint(rSegment, shiftSegment);
                        Vector2D currShift = new Vector2D(shiftSegment.GetVector().GetPoint(), intersection);
                        if (minShift == null)
                        {
                            minShift = currShift;
                        }
                        else
                        {
                            if (currShift.Module() < minShift.Module())
                                minShift = currShift;
                        }
                    }
                }
            }

            result = minShift;

            return result;
        }

    }
}
